package com.gexin.weixin.controller;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.StringUtil;
import com.gexin.weixin.kit.WxConfigKit;
import com.jfinal.core.NotAction;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.weixin.sdk.api.*;
import com.jfinal.weixin.sdk.api.JsTicketApi.JsApiType;
import com.jfinal.weixin.sdk.jfinal.ApiController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@ControllerBind(controllerKey = "/weixin/jssdk", viewPath = "WEB-INF/extend/weixin/")
public class WexinJsSdkController extends ApiController {
	private static final Log log = Log.getLog(WexinJsSdkController.class);

	// 授权拿到用户的详细信息
	public void index() {
		String url = getRequest().getHeader("Referer");
		if (StrKit.notBlank(url)) {
			url = url.split("#")[0];
		} else {
			log.error("请求地址不能为空");
			return;
		}
		Integer this_appid = getParaToInt("appid", 1); // 当前授权的
		ApiConfig config = WxConfigKit.getApiConfig(this_appid);
		if (config == null) {
			log.error("初始化JSSDK出错,预处理的appid：" + this_appid + "不存在！");
			renderNull();
			return;
		}
		ApiConfigKit.setThreadLocalAppId(config.getAppId());
		JsTicket jsApiTicket = JsTicketApi.getTicket(JsApiType.jsapi);
		String jsapi_ticket = jsApiTicket.getTicket();
		String nonce_str = create_nonce_str();
		String timestamp = create_timestamp();
		// 这里参数的顺序要按照 key 值 ASCII 码升序排序
		// 注意这里参数名必须全部小写，且必须有序
		String str = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "&timestamp=" + timestamp + "&url=" + url;
		String signature = HashKit.sha1(str);
/*		System.out.println("appId " + ApiConfigKit.getApiConfig().getAppId() + "  nonceStr " + nonce_str + " timestamp " + timestamp);
		System.out.println("url " + url + " signature " + signature);
		System.out.println("nonceStr " + nonce_str + " timestamp " + timestamp);
		System.out.println(" jsapi_ticket " + jsapi_ticket);
		System.out.println("nonce_str  " + nonce_str);*/
		setAttr("appId", ApiConfigKit.getApiConfig().getAppId());
		setAttr("nonceStr", nonce_str);
		setAttr("timestamp", timestamp);
		setAttr("url", url);
		setAttr("signature", signature);
		setAttr("jsapi_ticket", jsapi_ticket);
		setAttr("this_appid", this_appid);
		render("jssdk.js");
	}

	public void getSign() {
		String url = getRequest().getHeader("Referer");
		if (StringUtil.isBlank(url)) {
			url = getPara("url");
		}
		if (StrKit.notBlank(url)) {
			url = url.split("#")[0];
		} else {
			log.error("请求地址不能为空");
			renderJson(new ApiResponse(false,"请求地址不能为空"));
			return;
		}
		Integer this_appid = getParaToInt("appid", 1); // 当前授权的
		ApiConfig config = WxConfigKit.getApiConfig(this_appid);
		if (config == null) {
			log.error("初始化JSSDK出错,预处理的appid：" + this_appid + "不存在！");
			renderJson(new ApiResponse(false,"初始化JSSDK出错,预处理的appid：" + this_appid + "不存在！"));
			return;
		}

		ApiConfigKit.setThreadLocalAppId(config.getAppId());
		JsTicket jsApiTicket = JsTicketApi.getTicket(JsApiType.jsapi);
		String jsapi_ticket = jsApiTicket.getTicket();
		String nonce_str = create_nonce_str();
		String timestamp = create_timestamp();
		String str = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "&timestamp=" + timestamp + "&url=" + url;
		String signature = HashKit.sha1(str);
		Map<String, String> result_map = new HashMap<>();
		result_map.put("appId", ApiConfigKit.getApiConfig().getAppId());
		result_map.put("nonceStr", nonce_str);
		result_map.put("timestamp", timestamp);
		//result_map.put("url", url);
		result_map.put("signature", signature);
		result_map.put("jsapi_ticket", jsapi_ticket);
		result_map.put("this_appid", this_appid + "");
		ApiResponse resp = new ApiResponse();
		resp.setState(true);
		resp.setMsg("success");
		resp.setData(result_map);
		renderJson(resp);
	}

	@NotAction
	private static String create_timestamp() {
		return Long.toString(System.currentTimeMillis() / 1000);
	}

	@NotAction
	private static String create_nonce_str() {
		return UUID.randomUUID().toString();
	}
}
